#include "htf_native_model_parser.h"
#include "../../core/htf_resource_manager.h"
#include "../../utils/htf_format_detector.h"
#include <stdlib.h>  // for calloc, free

// Complete struct definition (visible only within this file)
struct htf_native_model {
    void* data;
    size_t size;
    int format;                // Assume that htf_detect_format returns an int
    htf_resource_t* resource;  // Assume that htf_resource_t has been declared in htf_resource_manager.h
};

htf_native_model_t* htf_native_parse_model(const char* path, const uint8_t* key, size_t key_len) {
    htf_resource_t* res = htf_load_resource_secure(path, key, key_len);
    if (!res) {
        return NULL;
    }

    htf_native_model_t* model = (htf_native_model_t*)calloc(1, sizeof(htf_native_model_t));
    if (!model) {
        htf_release_resource(res);
        return NULL;
    }

    model->data = res->data;
    model->size = res->size;
    model->format = htf_detect_format((uint8_t*)res->data, res->size);
    model->resource = res;

    return model;
}

void htf_native_free_model(htf_native_model_t* model) {
    if (model) {
        htf_release_resource(model->resource);
        free(model);
    }
}